/************************************************************************
 * @file UpdateSiteInputDialog.cpp
 * @author Ian Chen
 * @version 1.0
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
 * General Public License for more details at
 * http://www.gnu.org/licenses/lgpl.html
 *
 ************************************************************************/
#include <QFormLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMessageBox>

#include <pluginui/widget/UpdateSiteInputDialog.h>

#include <plugin/util/Framework.h>
#include <plugin/util/SiteManager.h>

namespace antu
{
namespace plugin
{
namespace ui
{
UpdateSiteInputDialog::UpdateSiteInputDialog(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout* pMainLayout = new QVBoxLayout(this);
    this->setLayout(pMainLayout);

    QFormLayout* pEntryLayout = new QFormLayout(this);
    this->m_pNameLineEdit = new QLineEdit(this);
    this->m_pUrlLineEdit = new QLineEdit(this);

    pEntryLayout->addRow(tr("Name:"), this->m_pNameLineEdit);
    pEntryLayout->addRow(tr("URL:"), this->m_pUrlLineEdit);
    pMainLayout->addLayout(pEntryLayout, 1);

    QHBoxLayout* pButtonLayout = new QHBoxLayout(this);
    pButtonLayout->addStretch(1);

    QPushButton* pOkButton = new QPushButton(tr("Ok"), this);
    QPushButton* pCancelButton = new QPushButton(tr("Cancel"), this);
    pButtonLayout->addWidget(pOkButton);
    pButtonLayout->addWidget(pCancelButton);

    pMainLayout->addLayout(pButtonLayout);

    this->connect(pOkButton, SIGNAL(clicked()), this, SLOT(okClicked()));
    this->connect(pCancelButton, SIGNAL(clicked()), this, SLOT(cancelClicked()));
}

void UpdateSiteInputDialog::okClicked()
{
    if (this->m_pNameLineEdit->text().isEmpty() ||
            this->m_pUrlLineEdit->text().isEmpty())
    {
        QMessageBox::critical(this, tr("Entries empty"), tr("Name and url fields cannot be empty."));
        return;
    }
    antu::plugin::Framework::Instance.siteManager()->addSite(
                    this->m_pNameLineEdit->text(),
                    this->m_pUrlLineEdit->text()
                );
    this->close();
}

void UpdateSiteInputDialog::cancelClicked()
{
    this->close();
}

}
}
}

